jQuery(document).ready(function($) {
   $('#interviewform #submit_button').click(function(){
      $('#interviewform #DEGREE').focus();
   });
   // Interview form action
   $("#interviewform").validate({
      rules: {
         WORK_EXP: {require_from_group: [1,".exp_group"],max:50},
         WORK_EXP_MONTHS: {require_from_group: [1,".exp_group"],max:12},
         INDUSTRY: {required: true}
      },
      groups:{
         workexp:"WORK_EXP WORK_EXP_MONTHS"
      },
      messages: {
         WORK_EXP: { max: "年限不能超过50" },
         WORK_EXP_MONTHS: { max: "月份不能超过12" }
      },
      errorPlacement:function(error,element) {
         if (element.attr("name") == "WORK_EXP" || element.attr("name") == "WORK_EXP_MONTHS")
            error.insertAfter("#WORK_EXP_MONTHS");
         else
            error.insertAfter(element);
      },
      submitHandler: function(form) {
         $("#interviewform").ajaxSubmit({
            dataType: "json",
               beforeSubmit: function(data, form, options) {
                  if (!showLoading("#interviewform", options, null, {button:true}))
                     return false;
                  $("#errormsg").hide();
               },
               success: function(data) {
                  try {
                     var retmsg = data;
                     if (retmsg.error) {
                        $("#errormsg").jobUtil("showError", retmsg.error);
                     }
                     if (retmsg.success) {
                        $("#errormsg").jobUtil("showSuccess", '提交成功').fadeIn('fast');
                        window.location = retmsg.redirect;
                     }
                  } catch(e) {
                     return false;
                  }
               }
         });
      }
   });

   // Form birth
   $("#birthday").datepicker({
      dateFormat: 'yy-mm-dd',
      changeYear: true,
      changeMonth: true,
      defaultDate: "-22y",
      yearRange: '-65:-18',
      numberOfMonths: 1,
      onClose: function(dateText) {
         if(dateText != $(this).attr("title"))
            $(this).removeClass("blur");
         else
            $(this).blur();
      }
   });

   $("#submit_button").prop('disabled',false);
   
   $("#giveupconfirm").validate({
      submitHandler: function(form) {
         $("#giveupconfirm").ajaxSubmit({
            dataType: "json",
            beforeSubmit: function(data, form, options) {
               if (!showLoading("#giveupconfirm"))
                  return false;
               $("#errormsg").hide();
            },
            success: function(data) {
               try {
                  var retmsg = data;
                  if (retmsg.error) {
                     $("#errormsg").jobUtil("showError", retmsg.error);
                  }
                  if (retmsg.success) {
                     $('.emailvalidation').hide();
                     $('.giveup_success').show();
                  }
               } catch(e) {
                  return false;
               }
            }
         });
      }
   });

   $(document).on('click','#addskill',function(){
      if ($("#skilldetails").css("display") == 'none') {
         $('#skilldetails').show();
      } else {
         var skillid = $(this).data('skillid') ? $(this).data('skillid') : 1; 
         // then assign it back 
         $(this).data('skillid', skillid+1);
 
         var html = '<div class="jos_owneditskill" data-skillid="'+skillid+'">';
             html += '<div class="jos_editdeleteskill medium" id="deleteskill"><a href="javascript:;">删除</a></div>';
             html += '<div class="hr_form_group clearfix">';
             html += '<label class="hr_name_label">';
             html += '技能名称：';
             html += '</label>';
             html += '<div class="hr_controls">';
             html += '<input type="text" class="input-small required" name="MORESKILL_NAME['+skillid+']"/>';
             html += '</div>';
             html += '</div>';
             html += '<div class="hr_form_group clearfix">';
             html += '<label class="hr_name_label">';
             html += '实际工作经验（月）：';
             html += '</label>';
             html += '<div class="hr_controls">';
             html += '<select id="moreskill'+skillid+'" class="exp_select required" name="MORESKILL_LEVEL['+skillid+']"><option value=>--选择熟练度--</option><option value=2>普通</option><option value=3>熟练</option><option value=4>精通</option></select>';
             html += ' <input id="moreskill'+skillid+'" type="text" class="input-small {required:true,digits:true,min:1,max:720,messages:{digits:\'只能输入正整数\', min:\'只能输入正整数\',max:\'实际工作经验不能超过720个月\'}}" name="MORESKILL_EXP['+skillid+']"/>';
             html += '<span>个月</span>';
             html += '<label class="error" style="display:none;" generated="true" for="moreskill'+skillid+'">该项必须填写</label>';
             html += '</div>';
             html += '</div>';
             html += '</div>';
         $("#skilldiv").append(html);
      }
   });

   $(document).on('click','#deleteskill a',function(){
      $(this).parent().parent().remove();
   });

   $(document).on('change', '.skill_ctl .exp_select', function(){
      var option = $(this).children('option:selected').val();
      var iid = $(this).data('iid');
      if (option == 0) {
         $(".skill_ctl .skill"+iid).removeClass('required').val('').prop('readonly',true);
      } else {
         $(".skill_ctl .skill"+iid).addClass('required').prop('readonly',false);
      }
   });
});
